﻿using System;
using System.Web.UI;
using Eneta.Portal.Common;
using Eneta.Portal.Common.Dto;
using Eneta.Portal.Common.Repository;
using Microsoft.SharePoint;

namespace Eneta.Portal.Controls
{
    public class HtmlDisplayControl : UserControl
    {
        public string Identification { get; set; }

        protected HtmlDto GetHtml()
        {
            try
            {
                if (string.IsNullOrEmpty(Identification))
                    return null;
                var repository = new HtmlRepository(SPContext.Current.Site.RootWeb);
                return repository.GetHtmlBy(Identification);
            }
            catch (Exception ex)
            {
                Logger.LogWarning("GetHtml(): Error occured", GetType().ToString());
                Logger.LogException(ex);
                return null;
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            var dto = GetHtml();
            if (dto == null)
                return;
            if (string.IsNullOrEmpty(dto.Html))
                return;

            writer.Write(dto.Html);
        }
    }
}